// pages/user/withdrawals.js
const config = require('../../config')
const util = require('../../utils/util')
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
    
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.showLoading({mask:true})
        this.getinfo()
    },
    saveform: function (e) {
        var formid = e.detail.formId
        app.saveformid(formid)
    }, 
    getinfo:function(){
        var that = this
        if (app.globalData.uid) {
            var headerkey = util.createheaderkey()
            wx.request({
                url: config.host + '/api/withdraw/withdrawinfo',
                method: 'GET',
                header: {
                    'content-type': 'application/json',
                    'key': headerkey.key,
                    'time': headerkey.timestamp
                },
                data: {
                    uid: app.globalData.uid
                },
                success: function (res) {
                    wx.hideLoading()
                    if (res.data.code == 200) {
                        var data = res.data.data
                        
                        that.setData({
                            balance: data.balance.balance,
                            rate: data.rate,
                            rule: data.rule
                        })
                    } else {
                        wx.showModal({
                            title: '失败',
                            content: res.data.msg,
                            showCancel: false
                        })
                    }
                },
                fail: function (res) {
                    wx.showToast({
                        title: '提现信息获取失败!',
                        duration: 3000
                    })
                }
            })
        } else {
            setTimeout(function () {
                that.getinfo()
            }, 1000)
        }
    },
    //修改提现金额
    changeamount:function(e){
        var value = parseFloat(e.detail.value)
        if (value){
            
            this.checkinput(value)
        }
        
        
    },
    //检查输入项
    checkinput: function (value) {
        var balance = parseFloat(this.data.balance),
            isint = / ^\d+$/,
            isfloat = /^\d+(\.\d+)?$/
            
        if (isint.test(value) || isfloat.test(value)) {
            console.log(value)
            value = value.toFixed(2)
            console.log(value)
            if (value >= 2) {
                
                    if (value > balance) {
                        wx.showToast({
                            title: '不能超过余额',
                        })
                        
                        this.countfee(balance)
                    } else {
                        this.countfee(value)

                    }
                
            } else {
                wx.showToast({
                    title: '最少提2元',
                })
                if (balance>2){
                    this.countfee(2)
                    this.setData({
                        amount: 2,
                    })
                }else{
                    this.setData({
                        amount: '',
                        poundage: ''
                    })
                }
                
            }
        } else {

            wx.showToast({
                title: '数据不合法',
            })
            this.setData({
                amount: '',
                poundage: '',
            })
        }
    },
    //计算手续费
    countfee: function (amount){
        var poundage = amount * parseFloat(this.data.rate),
            actual_amount = amount - poundage

        poundage = poundage.toFixed(2)
        actual_amount = actual_amount.toFixed(2)
        this.setData({
            amount: amount,
            actual_amount: actual_amount,
            poundage: poundage
        })
    },
    //全部提现
    allwithdrawals:function(){
        var that = this,
            balance = parseFloat(that.data.balance)
        if (balance>2){
            balance = balance.toFixed(2)
            this.countfee(balance)          
        }else{
            wx.showToast({
                title: '余额需大于2元',
            })
        }
    },
    //立刻提现
    withdrawals:function(loading = true) {
        
        var that = this
        var amount = that.data.amount,
            headerkey = util.createheaderkey()
        if (that.data.amount){
            wx.showLoading({
                title: '加载中',
                mask: true
            })
            wx.request({
                url: config.host + '/api/withdraw/index',
                method: 'POST',
                header: {
                    'content-type': 'application/json',
                    'key': headerkey.key,
                    'time': headerkey.timestamp
                },
                data: {
                    uid: app.globalData.uid,
                    amount: amount
                },
                success: function (res) {
                    wx.hideLoading()
                    if (res.data.code == 200) {
                        var data = res.data.data
                        wx.showModal({
                            title: '提现成功',
                            content: '提现金额:￥' + data.amount + ',手续费:￥' + data.poundage + '元，实际到账:￥' + data.actual_amount + '元',
                            showCancel:false,
                            success: function (res) {
                                wx.navigateBack()
                            }
                            
                        })
                        
                    }else{
                        wx.showModal({
                            title: '提现失败',
                            content: res.data.msg,
                            showCancel: false

                        })
                    }
                },
                fail: function (res) {
                    wx.hideLoading()
                }
            })
        } else if (loading){


            setTimeout(function () {
                that.withdrawals(false)
            }, 500)
            
        }else{
            wx.showToast({
                title: '请输入提现金额',
            })
        }
        

    },
    gotofaq:function(){
        wx.navigateTo({
            url: '/pages/user/faq'
        })
    },
    gotodetail:function(){
        wx.navigateTo({
            url: '/pages/user/amounttail'
        })
    }
})